package com.qiyang.uaa.service.impl;

import com.alibaba.fastjson.JSON;
import com.qiyang.common.utils.ChinsesUtils;
import com.qiyang.uaa.controller.AuthrizationController;
import com.qiyang.uaa.dao.SysRoleRepository;
import com.qiyang.uaa.dao.SysUserRepository;
import com.qiyang.uaa.dao.entity.SysRole;
import com.qiyang.uaa.dao.entity.SysUser;
import com.qiyang.uaa.service.AuthrizationService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * Created on 2018/5/31.
 *
 * @author sehuaqi.
 * 描述：
 */
@Service
public class AuthrizationServiceImpl implements AuthrizationService {

    Logger logger = LoggerFactory.getLogger(AuthrizationServiceImpl.class);

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysUserRepository sysUserRepository;

    @Override
    public List<SysRole> getRolesByUserId(Long userId) {

//        sysRoleRepository

        List<SysRole> sysRoleEntities = sysRoleRepository.findAll();

        System.out.println(JSON.toJSONString(sysRoleEntities));

        for (SysRole sysRole : sysRoleEntities) {

//            Set<SysUser> sysUserEntities = sysRole.getSysUsers();
//

//            System.out.println(JSON.toJSONString(sysUserEntities));
        }

        return null;
    }



    @Override
    public String initSysuser(int num) {
        for (int i = 0; i < num; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setUsername(RandomStringUtils.random(6, "abcdef0123456789"));
            sysUser.setEmail(sysUser.getUsername() + "@163.com");
            sysUser.setMobile("18" + RandomStringUtils.randomNumeric(9));
            sysUser.setNickname(ChinsesUtils.getRandomJianHan(3));
            sysUser.setPwd(RandomStringUtils.randomAlphabetic(16));
            sysUser.setSalt(RandomStringUtils.randomAlphanumeric(5));
            sysUser.setStatus((RandomUtils.nextInt(99) % 3));
            sysUserRepository.save(sysUser);
            logger.info("执行中=========>" + i);
        }
        sysUserRepository.flush();

        return "complete……";
    }
}
